package cn.xing.managementSystem.controller;

import cn.xing.managementSystem.bean.ResponseContent;
import cn.xing.managementSystem.bean.pageable.PageableRequest;
import cn.xing.managementSystem.bean.pageable.PageableResponse;
import cn.xing.managementSystem.domain.Course;
import cn.xing.managementSystem.domain.ImageStore;
import cn.xing.managementSystem.enums.CourseType;
import cn.xing.managementSystem.service.CourseService;
import cn.xing.managementSystem.service.ImageStoreService;
import cn.xing.managementSystem.utils.Preconditions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author cuixing
 * @package cn.xing.managementSystem.controller
 * @class CourseManagement
 * @email xing.cui@magicwindow.cn
 * @date 2018/4/24 上午10:22
 * @description
 */

@Controller
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Value("${image.boot.path}")
    private String imageBootPath;

    @Value("${image.url}")
    private String imageUrl;

    @RequestMapping("/courseList")
    public String courseList(){
        return "content/courseList";
    }

    @RequestMapping("/coursePageItems")
    @ResponseBody
    public ModelAndView coursePageItems(PageableRequest pageableRequest){
        ModelAndView mv = new ModelAndView("content/courseItems");
        PageableResponse<Course> pageableResponse = courseService.listPageable(pageableRequest);
        mv.addObject("pageableResponse", pageableResponse);
        return mv;
    }

    @RequestMapping("/editCourse")
    public ModelAndView editCourse(Long courseId){
        Course course = null;
        ModelAndView mv = new ModelAndView();
        if(Preconditions.isNotBlank(courseId)){
            course = courseService.findById(courseId);
        }else{
            course = new Course();
        }
        mv.addObject("course", course);
        mv.setViewName("content/editCourse");
        return mv;
    }


    @RequestMapping("/saveCourse")
    @ResponseBody
    public ResponseContent saveCourse(Long courseId, CourseType courseType, String courseName, String courseDetail, String courseIntroduction,
                                      Long courseImageId, @RequestParam("courseImageFile") MultipartFile courseImageFile) throws IOException {
        Course course;
        if(Preconditions.isNotBlank(courseId)){
            course = courseService.findById(courseId);
        }else{
            course = new Course();
        }
        course.setCourseName(courseName);
        course.setCourseType(courseType);
        course.setCourseIntroduction(courseIntroduction);
        course.setCourseDetail(courseDetail);
        if(courseService.saveCourseAndImageStore(course, courseImageId, courseImageFile)){
            return ResponseContent.buildSuccess("保存成功");
        }
        return ResponseContent.buildFail("保存失败");
    }


    @RequestMapping("/deleteCourseById")
    @ResponseBody
    public ResponseContent deleteCourseById(Long courseId) {
        Course course = courseService.findById(courseId);
        course.setDeleted(true);
        course = courseService.saveCourse(course);
        if(Preconditions.isNotBlank(course)){
            return ResponseContent.buildSuccess("删除成功");
        }
        return ResponseContent.buildFail("删除失败");
    }


    @RequestMapping("/v1/getCourseListByType/{type}")
    @ResponseBody
    public ResponseContent<JSONArray> getCourseList(@PathVariable CourseType type){
        List<Course> courses = courseService.findCourseListByCourseType(type);
        JSONArray jsonArray = new JSONArray();
        if(Preconditions.isNotBlank(courses)){
            for(Course course : courses){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", course.getId());
                jsonObject.put("course_name", course.getCourseName());
                jsonObject.put("course_type", course.getCourseType());
                jsonObject.put("image_url", String.format(imageUrl, course.getImageStore().getId()));
                jsonObject.put("course_introduction", course.getCourseIntroduction());
                jsonArray.add(jsonObject);
            }
        }
        return ResponseContent.buildSuccess(null, jsonArray);
    }

    @RequestMapping("/v1/getCourseDetailById/{id}")
    @ResponseBody
    public ResponseContent<Course> getCourseDetailById(@PathVariable Long id){
        Course course = courseService.findById(id);
        JSONObject jsonObject = new JSONObject();
        if(Preconditions.isNotBlank(course)){
            jsonObject.put("id", course.getId());
            jsonObject.put("course_name", course.getCourseName());
            jsonObject.put("course_detail", course.getCourseDetail());
        }
        return ResponseContent.buildSuccess(null, jsonObject);
    }

}